import { useQuery } from '@tanstack/vue-query'
import { type MaybeRef, computed, isRef } from 'vue'
import type { ClassStudentQueryKey } from './types'
import { ApiGetClassStudents } from '.'

export function useClassStudentQueryKey(id: MaybeRef<number>): MaybeRef<ClassStudentQueryKey> {
  return isRef(id)
    ? computed(() => ['get', 'classes', id.value, 'students'])
    : ['get', 'classes', id, 'students']
}
export function useClassStudents(id: MaybeRef<number>) {
  return useQuery({
    staleTime: 5 * 60_000,
    queryKey: useClassStudentQueryKey(id),
    queryFn: ({ queryKey: [, , id] }) => ApiGetClassStudents(id),
    select: ({ data }) => data
  })
}
